perm filename CYCLIC.LSP[E80,JMC] blob sn#534930 filedate 1980-09-12 generic text, type C, neo UTF8
COMMENT āŠ—   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	 functions for computing with cyclic list structures
C00004 ENDMK
CāŠ—;
;;; functions for computing with cyclic list structures

(defun count (x) (car (count1 x nil)))

;;; This seems right, but version in cyclic[e80,jmc] probably is
;;; more complicated for some reason.
(defun count1 (x u) (if
			(atom x) (cons 1 u)
			(member x u) (cons 0 u)
			((lambda (w) (foo (car w) (count1 (cdr x) (cdr w))))
				(count1 (car x) (cons x u)))))

(defun foo (n u) (cons (+ n (car u)) (cdr u)))

(defun cycle1 (u v) (if (null (cdr u)) (rplacd u v) (cycle1 (cdr u) v)))

(defun cycle (u) (cycle1 u u))

((lambda (x) nil)(setq a (cycle '(a b c))))

(defun nodes1 (x u) (if (member x u)	u
			(atom x)	(cons x u)
			(nodes1 (cdr x) (nodes1 (car x) (cons x u)))))

(defun count2 (x) (length (nodes1 x nil)))

;;; Carolyn's idea for counting and listing at once.
;;; The argument is a list <<node> <count> <seen>>
(defun nodesc (w) (if
	(member (car w) (caddr w)) 
	(list (cadr w) (caddr w))
	(atom (car w))
	 (cons (add1 (cadr w)) (list (cons (car w) (caddr w))))
	 (nodesc (cons (car (car w))
                       (nodesc (foo w))))))

(progn (setq z (list ((lambda (w) (rplaca w w)) '(a.b)) 0 nil)) 0) 

(setq zz '((a.a) 0 nil))

(defun foo (w) (list (cdr (car w)) (add1 (cadr w)) (cons (car w) (caddr w))))